#include "VorestHtmlReader.h"

using namespace dataengine;
using namespace std;

VorestHtmlReader::VorestHtmlReader(std::string fileName) : m_fileName(fileName)
{
	m_file.open(fileName.c_str());
  	m_file.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
}

bool VorestHtmlReader::canLoadFile()
{
	if(!m_file)
	{
		printf("Could not open file\n");
		return false;
	}
	return true;
	///---------------------------------------
	// FIXME: Esto es lo que deberia hacer, pero da problemas por asuntos de encodings (UTF8, latin1, etc)
  	m_file.seekg (0, ios::beg);
	char firstLine[2048];
	m_file.getline (firstLine, 2048);

	string header("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=latin1\"><title>Tabla de datos</title><style>");

	int val;
	if( string(firstLine) == header)
		return true;
	printf("COMP VAL: %i, HEADER READ: %s\n",val, firstLine);

	return false;
}

std::vector<VorestTreeInfo> VorestHtmlReader::readFile()
{
  	m_file.seekg (0, ios::beg);
	// We skip the HTML header and body
	string header = getNextLine();
	string body = getNextLine();
	string firstTR = getNextLine();
	int tokType;
	// We skip the table header
	do{
		string currentLine = getNextLine();
		tokType = tokenType(currentLine);
	}while(tokType != Token_TR_End);
	// Now we are at the beggining of the first tree info

	vector<VorestTreeInfo> treeInfos;
	while(tokenType(getNextLine()) == Token_TR)
	{
		treeInfos.push_back(readNextTreeInfo());
	}

	return treeInfos;
}

std::string VorestHtmlReader::getNextLine()
{
	char line[2048];
	m_file.getline (line, 2048);
	return string(line);
}

int VorestHtmlReader::tokenType(const std::string& line)
{
	string tokenID("");
	const char * it = line.c_str();
	while(*it != '<') // we search for the opening token char
	{
		if(*it == 0)
			return Token_Other; // We got out of the c_str

		++it;
	}
	++it; // we go past the <
	while((*it != '>') && (*it != ' ')) // We add all chars until the token closing char
	{
		if(*it == 0)
			return Token_Other; // We got out of the c_str

		tokenID+=*it;
		++it;
	}

	if(tokenID == "TD")
		return Token_TD;
	if(tokenID == "TR")
		return Token_TR;
	if(tokenID == "/TR")
		return Token_TR_End;
	return Token_Other;
}

std::string VorestHtmlReader::getTDContents(const std::string &line)
{
	if(tokenType(line) != Token_TD)
		throw HtmlParseException(string("Unexpected token type. Expected Token_TD, and received line: ")+line);

	string contents("");
	const char * it = line.c_str();
	while(*it != '>') // we search for the first token closing char
	{
		if(*it == 0)
			return contents; // We got out of the c_str
		++it;
	}
	++it; // we go past the closing char
	while((*it != '<') && (*it != 0)) // We add all chars until the token closing char
	{
		contents+=*it;
		++it;
	}
	return contents; 
}

VorestTreeInfo VorestHtmlReader::readNextTreeInfo()
{
	string token = getTDContents(getNextLine());
	VorestTreeInfo info;
	info.m_treeID = atoi(token.c_str());
	info.m_position.x = atof(getTDContents(getNextLine()).c_str());
	info.m_position.y = atof(getTDContents(getNextLine()).c_str());
	info.m_position.z = atof(getTDContents(getNextLine()).c_str());
	info.m_treeDiameter = atof(getTDContents(getNextLine()).c_str());// /25);
	info.m_length= atof(getTDContents(getNextLine()).c_str());
	
	info.m_baseColor.dx = atof(getTDContents(getNextLine()).c_str());
	info.m_baseColor.dy= atof(getTDContents(getNextLine()).c_str());
	info.m_baseColor.dz= atof(getTDContents(getNextLine()).c_str());

	info.m_rotation = atof(getTDContents(getNextLine()).c_str());
	info.m_area = atof(getTDContents(getNextLine()).c_str());

	info.m_speciesID = atoi(getTDContents(getNextLine()).c_str());

	info.m_treeHeight = atof(getTDContents(getNextLine()).c_str());
	info.m_globalHeight = atof(getTDContents(getNextLine()).c_str());
	info.m_isAlive = atoi(getTDContents(getNextLine()).c_str());

	info.m_modelID = 0; // to support .vib files with multiple models. It defaults to 0, as this info is not generated by Vorest

	while(tokenType(getNextLine()) != Token_TR_End);

	return info;
}

